package de.tum.in.eist.poll.server.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import de.tum.in.eist.poll.shared.beans.Feedback;
import de.tum.in.eist.poll.shared.beans.StudentLectureKey;

/**
 * This is an in-memory implementation of the {@link FeedbackDaoInterface}.
 * 
 * @author The EIST-POLL-21 team.
 *
 */
public class FeedbackDaoImpl implements FeedbackDaoInterface, Serializable {
	/**
	 * serialization version
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	HashMap<StudentLectureKey,Feedback> feedbackMap =
		new HashMap<StudentLectureKey,Feedback>();
	
	public FeedbackDaoImpl() {
	}

	@Override
	public void add(Feedback feedback) {
		feedbackMap.put(new StudentLectureKey(feedback.getStudentID(),
				feedback.getLectureID()), feedback);
	}

	@Override
	public Feedback get(String lectureID, String studentID) {
		return feedbackMap.get(new StudentLectureKey(studentID, lectureID));
	}

	@Override
	public List<Feedback> getByStudent(String studentID) {
		// TODO LONG: this is kinda inefficient...
		ArrayList<Feedback> result = new ArrayList<Feedback>();
		Set<StudentLectureKey> keySet = feedbackMap.keySet();
		for (StudentLectureKey key : keySet) {
			if (key.getStudentID().equals(studentID))
				result.add(feedbackMap.get(key));
		}
		return result;
	}

	@Override
	public List<Feedback> getByLecture(String lectureID) {
		// TODO LONG: this is kinda inefficient...
		ArrayList<Feedback> result = new ArrayList<Feedback>();
		Set<StudentLectureKey> keySet = feedbackMap.keySet();
		for (StudentLectureKey key : keySet) {
			if (key.getLectureID().equals(lectureID))
				result.add(feedbackMap.get(key));
		}
		return result;
	}
}

